package com.central.scheduler.spi.task;
import com.central.scheduler.spi.task.request.TaskRequest;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Tindy
 * @date 2021/10/29
 * @describe
 */
public class TaskExecutionContextCacheManager {
    /**
     * taskInstance cache
     */
    private static Map<Long, TaskRequest> taskRequestContextCache = new ConcurrentHashMap<>();

    /**
     * cache taskInstance
     *
     * @param request request
     */
    public static void cacheTaskExecutionContext(TaskRequest request) {
        taskRequestContextCache.put(request.getTaskInstanceId(), request);
    }
    /**
     * remove taskInstance by taskInstanceId
     *
     * @param taskInstanceId taskInstanceId
     */
    public static void removeByTaskInstanceId(Long taskInstanceId) {
        taskRequestContextCache.remove(taskInstanceId);
    }
    /**
     * get taskInstance by taskInstance id
     *
     * @param taskInstanceId taskInstanceId
     * @return taskInstance
     */

    public static TaskRequest getByTaskInstanceId(Long taskInstanceId) {
        return taskRequestContextCache.get(taskInstanceId);
    }

    public static boolean updateTaskExecutionContext(TaskRequest request) {
        taskRequestContextCache.computeIfPresent(request.getTaskInstanceId(), (k, v) -> request);
        return taskRequestContextCache.containsKey(request.getTaskInstanceId());
    }
}
